-- Winter Anathema (c) v.v.balashoff
local Color = require('libs/colorlibrary/colorlibrary')
local Domain = require('leviathan/ECS/Domain')
local Entity = require('leviathan/ECS/Entity')
local FPSComponent = require('leviathan/ECS/UI/Components/FPSComponent')
local i18n = require('libs/i18n')
local TextComponent = require('leviathan/ECS/UI/Components/TextComponent')
local UIFPSSystem = require('leviathan/ECS/UI/Systems/FPSSystem')
local UIRenderSystem = require('leviathan/ECS/UI/Systems/RenderSystem')

local DomainFPS = Domain:derive('FPS')

function DomainFPS:new(world)
    -- call parent constructor
    DomainFPS.super.new(self, world, 'FPS')
    -- screen dimensions
    local screenW, screenH = love.window.getMode()
    -- get managers
    local FontsManager = world:getManager('FontsManager')
    self.fontSize = math.floor(screenH / 45)
    self.font = FontsManager:getFont('TypeWriter', self.fontSize)
    w, h = FontsManager:getTextSize(self.font, '000', screenW)

    -- set buffer size
    self.buffer.worldX = math.floor(screenW - w)
    self.buffer.worldY = math.floor(self.fontSize / 10)
    self.buffer.w = w
    self.buffer.h = h

    -- entity: fps
    local entity = Entity(self, 'FPS', 0, 0, w, h)
    -- components
    local text = TextComponent('FPS', 0, 0, w, h, '000', self.font, Color.white, 'left', Color.blue)
    entity:addComponent(text)
    local fps = FPSComponent(text)
    entity:addComponent(fps)
    -- systems
    local fpssys = UIFPSSystem(self)
    local render = UIRenderSystem(self)
    DomainFPS:addSystem(self, fpssys)
    DomainFPS:addSystem(self, render)
end

return DomainFPS
